package net.poemcode.gzlib;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

import static net.poemcode.gzlib.LibraryOpenHelper.*; 

public class LibraryProvider extends ContentProvider {
	
    private static final int CODE_LIB_ALL = 1;
    private static final int CODE_LIB_SPECIFIC = 2;
    private static final int CODE_LOCAL_ALL = 3;
    private static final int CODE_LOCAL_SPECIFIC = 4;
    private static final int CODE_CIR_ALL = 5;
    private static final int CODE_CIR_SPECIFIC = 6;
    private static final int CODE_STATE_ALL = 7;
    private static final int CODE_STATE_SPECIFIC = 8;
    
    private SQLiteOpenHelper sqliteHelper;
    
	private static final UriMatcher sMatcher;
	static {  
        sMatcher = new UriMatcher(UriMatcher.NO_MATCH);  
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_LIB, CODE_LIB_ALL);  
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_LIB + "/#", CODE_LIB_SPECIFIC);  
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_LOCAL, CODE_LOCAL_ALL);
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_LOCAL + "/#", CODE_LOCAL_SPECIFIC);  
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_CIR, CODE_CIR_ALL);  
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_CIR + "/#", CODE_CIR_SPECIFIC);
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_STATE, CODE_STATE_ALL);  
        sMatcher.addURI(URI_AUTHORITY, TABLE_NAME_STATE + "/#", CODE_STATE_SPECIFIC);  
    }  

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = sqliteHelper.getWritableDatabase();
		int result = 0;
		switch (sMatcher.match(uri)) {
		case CODE_LIB_ALL:
			result = db.delete(TABLE_NAME_LIB, "1", null);
			break;
		case CODE_LIB_SPECIFIC:
			result = db.delete(TABLE_NAME_LIB, selection, selectionArgs);
			break;
		case CODE_LOCAL_ALL:
			result = db.delete(TABLE_NAME_LOCAL, "1", null);
			break;
		case CODE_LOCAL_SPECIFIC:
			result = db.delete(TABLE_NAME_LOCAL, selection, selectionArgs);
			break;
		case CODE_CIR_ALL:
			result = db.delete(TABLE_NAME_CIR, "1", null);
			break;
		case CODE_CIR_SPECIFIC:
			result = db.delete(TABLE_NAME_CIR, selection, selectionArgs);
			break;
		case CODE_STATE_ALL:
			result = db.delete(TABLE_NAME_STATE, "1", null);
			break;
		case CODE_STATE_SPECIFIC:
			result = db.delete(TABLE_NAME_STATE, selection, selectionArgs);	
			break;
		default:
			break;
		}
		
		return result;
	}

	@Override
	public String getType(Uri uri) {
		// 暂时不实现
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = sqliteHelper.getWritableDatabase();
		long id = 0;
		switch (sMatcher.match(uri)) {
		case CODE_LIB_ALL:
			id = db.insert(TABLE_NAME_LIB, null, values);
			if (id > 0) {
	            Uri insertUserUri = ContentUris.withAppendedId(CONTENT_URI_LIB, id);
	            getContext().getContentResolver().notifyChange(insertUserUri, null);
	            return insertUserUri;
	        } else {
	        	return null;
	        }
		case CODE_LOCAL_ALL:
			id = db.insert(TABLE_NAME_LOCAL, null, values);
			if (id > 0) {
	            Uri insertUserUri = ContentUris.withAppendedId(CONTENT_URI_LOCAL, id);
	            getContext().getContentResolver().notifyChange(insertUserUri, null);
	            return insertUserUri;
	        } else {
	        	return null;
	        }
		case CODE_CIR_ALL:
			id = db.insert(TABLE_NAME_CIR, null, values);
			if (id > 0) {
	            Uri insertUserUri = ContentUris.withAppendedId(CONTENT_URI_CIR, id);
	            getContext().getContentResolver().notifyChange(insertUserUri, null);
	            return insertUserUri;
	        } else {
	        	return null;
	        }
		case CODE_STATE_ALL:
			id = db.insert(TABLE_NAME_STATE, null, values);
			if (id > 0) {
	            Uri insertUserUri = ContentUris.withAppendedId(CONTENT_URI_STATE, id);
	            getContext().getContentResolver().notifyChange(insertUserUri, null);
	            return insertUserUri;
	        } else {
	        	return null;
	        }
		default:
			return null;
		}
		
	}

	@Override
	public boolean onCreate() {
		sqliteHelper = new LibraryOpenHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		String tableName;
		switch (sMatcher.match(uri)) {
		case CODE_LIB_ALL:
			tableName = TABLE_NAME_LIB;
			break;
		case CODE_LIB_SPECIFIC:
			tableName = TABLE_NAME_LIB;
			break;
		case CODE_LOCAL_ALL:
			tableName = TABLE_NAME_LOCAL;
			break;
		case CODE_LOCAL_SPECIFIC:
			tableName = TABLE_NAME_LOCAL;
			break;
		case CODE_CIR_ALL:
			tableName = TABLE_NAME_CIR;
			break;
		case CODE_CIR_SPECIFIC:
			tableName = TABLE_NAME_CIR;
			break;
		case CODE_STATE_ALL:
			tableName = TABLE_NAME_STATE;
			break;
		case CODE_STATE_SPECIFIC:
			tableName = TABLE_NAME_STATE;	
			break;
		default:
			return null;
		}
		
		SQLiteDatabase db = sqliteHelper.getReadableDatabase();
		Cursor result = db.query(tableName, new String[]{COLUMN_CODE, COLUMN_NAME}, selection, selectionArgs, null, null, null);
		return result;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		String tableName;
		switch (sMatcher.match(uri)) {
		case CODE_LIB_ALL:
			tableName = TABLE_NAME_LIB;
			break;
		case CODE_LIB_SPECIFIC:
			tableName = TABLE_NAME_LIB;
			break;
		case CODE_LOCAL_ALL:
			tableName = TABLE_NAME_LOCAL;
			break;
		case CODE_LOCAL_SPECIFIC:
			tableName = TABLE_NAME_LOCAL;
			break;
		case CODE_CIR_ALL:
			tableName = TABLE_NAME_CIR;
			break;
		case CODE_CIR_SPECIFIC:
			tableName = TABLE_NAME_CIR;
			break;
		case CODE_STATE_ALL:
			tableName = TABLE_NAME_STATE;
			break;
		case CODE_STATE_SPECIFIC:
			tableName = TABLE_NAME_STATE;	
			break;
		default:
			return 0;
		}
		
		SQLiteDatabase db = sqliteHelper.getWritableDatabase();
		int result = db.update(tableName, values, selection, selectionArgs);
		return result;
	}

}
